package com.ljl.user.controller;


import com.ljl.basic.exception.BusinessException;
import com.ljl.basic.util.AjaxResult;
import com.ljl.user.dto.BinderDto;
import com.ljl.user.dto.LoginDto;
import com.ljl.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;


    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            return logininfoService.accountLogin(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后重试!!!").setSuccess(false);
        }

    }

    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后重试!!!").setSuccess(false);
        }

    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody BinderDto binderDto){
        try {
            return logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后重试!!!").setSuccess(false);
        }

    }

    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable String token){
        try {
            logininfoService.logout(token);
            return new AjaxResult();
        }  catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后重试!!!").setSuccess(false);
        }

    }

}
